'use strict';

const mysql = require('mysql');
const MySQL = require('../../../../Config').MySql.meter;
const log = require('../../SysLog/OutPut');

const pool = mysql.createPool({
    host: MySQL.host,
    port: MySQL.port,
    user: MySQL.user,
    password: MySQL.password,
    database: MySQL.database,
    connectionLimit: MySQL.connectionLimit,
    debug: MySQL.dbDebug,
    multipleStatements: MySQL.multipleStatements,
});

function wrapper(client) {
    let query = client.query;
    let o = {};
    o.query = function () {
        let args = [].slice.call(arguments);
        return new Promise(function (resolve, reject) {
            args.push(function (err, rows) {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                    log.debug(this.sql, 'SQL');
                }
            });
            query.apply(client, args);
        });
    };
    return o;
}

module.exports = wrapper(pool).query;
